﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MovieCollectionManager.DataModel;

namespace MovieCollectionManager
{
    public partial class SearchForm : Form
    {
        public SearchForm()
        {
            InitializeComponent();
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            listBoxSearchResult.DataSource = null;
            listBoxSearchResult.Items.Clear();
            string movieType = "";
            if (!comboBoxType.Text.Equals("(Todos)"))
                movieType = comboBoxType.Text;

            using (var db = new MovieCollectionConteiner())
            {
                var moviesResult = (from movie in db.Movies
                                    where movie.Title.Contains(textBoxTitle.Text)
                                          && movie.Type.Contains(movieType)
                                          && ((movie.ReleaseDate >= numericUpDownStartingYear.Value) && (movie.ReleaseDate <= numericUpDownEndingYear.Value))
                                          && ((movie.Rate >= (double)numericUpDownStartingRate.Value) && (movie.Rate <= (double) numericUpDownEndingRate.Value))
                                    select movie);

                if (moviesResult.Count() == 0)
                    return;

                listBoxSearchResult.DataSource = moviesResult.ToList();
                listBoxSearchResult.DisplayMember = "Title";
                listBoxSearchResult.ValueMember = "Id";
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonDetails_Click(object sender, EventArgs e)
        {
            if (listBoxSearchResult.SelectedIndex != -1)
            {
                Movie movie = (Movie)listBoxSearchResult.SelectedItem;
                DetailsForm detailsForm = new DetailsForm(movie);
                detailsForm.ShowDialog();
            }
        }

        private void SearchForm_Load(object sender, EventArgs e)
        {
            numericUpDownEndingYear.Value = DateTime.Now.Year;
            comboBoxType.SelectedIndex = 0;
        }
    }
}
